package com.egao.orderB.service.impl;

import com.egao.common.core.constants.UserConstant;
import com.egao.common.core.utils.Utils;
import com.egao.common.core.utils.messages.PhoneUtils;
import com.egao.common.core.web.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.biz.UserBiz;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.CustomerMapper;
import com.egao.common.system.service.UserService;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.CustomerCopy;
import com.egao.orderB.service.CustomerService;
import net.sf.json.JSONObject;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;
import com.egao.orderB.biz.CustomerBiz;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户管理服务实现类
 * @author : Doman
 * @date : 2020-06-02 13:34:13
 * @version ：1.0
 */
@Service
public class CustomerServiceImpl /*extends ServiceImpl<CustomerMapper, Customer> */implements CustomerService {

	@Autowired
	private CustomerBiz customerBiz;
	@Autowired
	private UserBiz userBiz;
    @Autowired
    private UserService userService;

    /**
    @Override
    public PageResult<Customer> listPage(PageParam<Customer> page) {
        List<Customer> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<Customer> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JSONObject list(PageResult2<T> page, Customer customer, User loginUser) throws Exception {
        return customerBiz.list(page,customer,loginUser);
    }

    @Override
    public List<Customer> list(Customer customer, User loginUser) {
        return customerBiz.list(customer,loginUser);
    }

    @Override
    public Customer getById(Customer customer) {
        return customerBiz.getById(customer);
    }

    @Transactional
    @Override
    public JsonResult save(Customer customer, User loginUser) {
        if (customerBiz.save(customer, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult ApiSave(CustomerCopy customer, User loginUser, User user) {
        customer.setCompanyId(UserConstant.COMPANY_ID + "");
        customer.setState("未审核");
        customer.setSettle("月结");
        customer.setAdminUser(user.getUsername());
        Customer cc = new Customer();
        cc.setName(customer.getName());
        List<Customer> customerList = customerBiz.getByName(cc);
        if(customerList != null && customerList.size() > 0) {
            return JsonResult.error("注册失败,公司名称已经注册!");
        }
        int key= customerBiz.ApiSave(customer, loginUser);
        user.setCompanyId(UserConstant.COMPANY_ID);
        user.setCustomerId(key);
        user.setType("客户");
        user.setAdmin("1");
        user.setState(2);
        if (Utils.isEmpty(user.getPhone())) {
            user.setPhone(user.getUsername());
        }
        user.setPassword(userService.encodePsw(user.getPassword()));
        key= userService.apiSave(user);
        user = new User();
        user.setUserId(key);
        user.setRoleId(UserConstant.CUSTOMER_ROLE_ID);
        userService.saveUserRole(user);
        return JsonResult.ok("注册成功");
    }

    @Transactional
    @Override
    public JsonResult update(Customer customer, User loginUser) {
        if (customerBiz.update(customer, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(Customer customer, User loginUser) {
        if (customerBiz.remove(customer, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }
    @Transactional
    @Override
    public JsonResult examine(Customer customer, User loginUser) {
        Customer customer2 = customerBiz.getById(customer);
        User user = userService.apiGetByUsername(customer2.getAdminUser());
        user.setState(0);
        userBiz.apiExamine(user, loginUser);
        customer.setState("已审核");
        if (customerBiz.examine(customer, loginUser)) {
            PhoneUtils.sendMsg(user.getUsername(), "600424", new String[]{user.getUsername()});
            return JsonResult.ok("审核成功");
        }
        return JsonResult.error("审核失败");
    }

    @Transactional
    @Override
    public JsonResult examineFail(Customer customer, User loginUser) {
        if (customerBiz.examineFail(customer, loginUser)) {
            return JsonResult.ok("审核成功");
        }
        Customer customer2 = customerBiz.getById(customer);
        PhoneUtils.sendMsg(customer2.getAdminUser(), "600431", new String[]{customer2.getAdminUser()});
        return JsonResult.error("审核失败");
    }

}
